package bookmanagementsystem.gui;

import bookmanagementsystem.model.ReturningRecord;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class ReturningRecordsFrame extends JFrame {
    private JList<String> returningList;

    public ReturningRecordsFrame(List<ReturningRecord> returningRecords) {
        setTitle("还书记录");
        setSize(500, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        initComponents(returningRecords);
        addComponentsToFrame();
    }

    private void initComponents(List<ReturningRecord> returningRecords) {
        List<String> recordInfoList = new ArrayList<>();
        if (returningRecords != null && !returningRecords.isEmpty()) {
            for (ReturningRecord record : returningRecords) {
                String recordInfo = "还书记录ID：" + record.getRecordID() + "\n" +
                        "图书ID：" + record.getBookID() + "\n" +
                        "归还时间：" + record.getReturningTime() + "\n\n";
                recordInfoList.add(recordInfo);
            }
        }
        returningList = new JList<>(recordInfoList.toArray(new String[0])); // 直接使用数组创建JList
    }

    private void addComponentsToFrame() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(new JLabel("您的还书记录如下："), BorderLayout.NORTH);
        panel.add(new JScrollPane(returningList), BorderLayout.CENTER);
        getContentPane().add(panel);
    }
}